//
//  YLSliderVerificationView.swift
//  YLIMKeyBoard
//
//  Created by AlbertYuan on 2023/2/27.
//

import UIKit

public class YLSliderVerificationView: UIView {

    @IBOutlet weak var sliderView: YLVerificationCodeView!
    @IBOutlet weak var backView: UIView!

    public static var isshowing:Bool = false

    public var dataCallback:((_ param:[String:String]) ->())?

    //显示操作：外部直接调用
    public static func show(complete:((_ param:[String:String]) ->())?){
        //防止重复显示
        if YLSliderVerificationView.isshowing{
            return
        }
        YLSliderVerificationView.isshowing = true
        //显示
        if let  codesliseer = createNewSliderView(){
            codesliseer.dataCallback = complete
            k_currentWindow?.addSubview(codesliseer)
        }
    }

    override public func awakeFromNib() {
        super.awakeFromNib()
        autoresizingMask = UIView.AutoresizingMask()
        configUI()
    }

    public func configUI(){
        backgroundColor = UIColor(hex: "#04040F")?.withAlphaComponent(0.36)
        backView.layer.cornerRadius = 8
        sliderView.callBack = {[weak self] param in
            self?.dataCallback?(param)
            self?.hide()
        }
    }
    
    public func addTopGesgure(){
        let tap = UITapGestureRecognizer(target: self, action: #selector(clickMaskToHidden))
        self.addGestureRecognizer(tap)
    }

    @objc public func clickMaskToHidden(){
        //不提供蒙版隐藏操作
        //hide()
    }

    //构建滑块
    static private func createNewSliderView()->YLSliderVerificationView?{
        let codeslide =  Bundle.main.loadNibNamed("YLSliderVerificationView", owner: nil)?.first as? YLSliderVerificationView
        codeslide?.frame = UIScreen.main.bounds
        return codeslide
    }

    //隐藏操作
    public func hide(){
        YLSliderVerificationView.isshowing  = false
        self.removeFromSuperview()
    }

}
